# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor
from qfluentwidgets import MessageBoxBase, SubtitleLabel, LineEdit, CaptionLabel


class InputDialog(MessageBoxBase):
    def __init__(self, title: str, parent=None):
        super().__init__(parent)
        self.titleLabel = SubtitleLabel(title, self)
        self.lineEdit = LineEdit(self)

        self.lineEdit.setPlaceholderText('请输入信息')
        self.lineEdit.setClearButtonEnabled(True)

        self.warningLabel = CaptionLabel("输入框不能为空", self)
        self.warningLabel.setTextColor("#cf1010", QColor(255, 28, 32))

        # add widget to view layout
        self.viewLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.viewLayout.addWidget(self.titleLabel)
        self.viewLayout.addSpacing(20)
        self.viewLayout.addWidget(self.lineEdit)
        self.viewLayout.addWidget(self.warningLabel)
        self.warningLabel.hide()

        # change the text of button
        self.yesButton.setText('打开')
        self.cancelButton.setText('取消')

        self.widget.setMinimumSize(600, 200)

    def validate(self):
        isValid = bool(self.lineEdit.text())
        self.warningLabel.setVisible(not isValid)
        self.lineEdit.setError(not isValid)
        return isValid

    def setPlaceholderText(self, text: str):
        self.lineEdit.setPlaceholderText(text)

    def text(self) -> str:
        return self.lineEdit.text()
